/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfoexam.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.modules.campusinfoexam.entity.CampusinfoTrainType;
import com.lczyfz.campusinfo.modules.campusinfoexam.service.CampusinfoTrainTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfoexam.entity.CampusinfoTrainName;
import com.lczyfz.campusinfo.modules.campusinfoexam.service.CampusinfoTrainNameService;
import java.util.List;
/**
 * 证照培训名称Controller
 * @author zms
 * @version 2018-04-15
 */
@Controller
@RequestMapping(value = "${adminPath}/campusinfoexam/campusinfoTrainName")
public class CampusinfoTrainNameController extends BaseController {

	@Autowired
	private CampusinfoTrainNameService campusinfoTrainNameService;

	@Autowired
	private CampusinfoTrainTypeService campusinfoTrainTypeService;
	@ModelAttribute
	public CampusinfoTrainName get(@RequestParam(required=false) String id) {
		CampusinfoTrainName entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = campusinfoTrainNameService.get(id);
		}
		if (entity == null){
			entity = new CampusinfoTrainName();
		}
		return entity;
	}
	
	@RequiresPermissions("campusinfoexam:campusinfoTrainName:view")
	@RequestMapping(value = {"list", ""})
	public String list(CampusinfoTrainName campusinfoTrainName, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CampusinfoTrainName> page = campusinfoTrainNameService.findPage(new Page<CampusinfoTrainName>(request, response), campusinfoTrainName);
		List<CampusinfoTrainName> campusinfoTrainNames = campusinfoTrainNameService.findList(campusinfoTrainName);
		model.addAttribute("campusinfoTrainNames",campusinfoTrainNames);
		model.addAttribute("page", page);
		return "modules/campusinfoexam/campusinfoTrainNameList";
	}

	@RequiresPermissions("campusinfoexam:campusinfoTrainName:view")
	@RequestMapping(value = "form")
	public String form(CampusinfoTrainName campusinfoTrainName, Model model) {
		CampusinfoTrainType campusinfoTrainType = new CampusinfoTrainType();
		List<CampusinfoTrainType> campusinfoExamTypes = campusinfoTrainTypeService.findList(campusinfoTrainType);
		model.addAttribute("campusinfoTrainTypes", campusinfoExamTypes);
		model.addAttribute("campusinfoTrainName", campusinfoTrainName);
		return "modules/campusinfoexam/campusinfoTrainNameForm";
	}

	@RequiresPermissions("campusinfoexam:campusinfoTrainName:edit")
	@RequestMapping(value = "save")
	public String save(CampusinfoTrainName campusinfoTrainName, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, campusinfoTrainName)){
			return form(campusinfoTrainName, model);
		}
		campusinfoTrainNameService.save(campusinfoTrainName);
		addMessage(redirectAttributes, "保存证照培训名称成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfoexam/campusinfoTrainName/?repage";
	}
	
	@RequiresPermissions("campusinfoexam:campusinfoTrainName:edit")
	@RequestMapping(value = "delete")
	public String delete(CampusinfoTrainName campusinfoTrainName, RedirectAttributes redirectAttributes) {
		campusinfoTrainNameService.delete(campusinfoTrainName);
		addMessage(redirectAttributes, "删除证照培训名称成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfoexam/campusinfoTrainName/?repage";
	}

}